// 这里是管理员相关的网络请求
import type { AxiosResponse } from 'axios'
import axios, { AxiosDefaultTypes } from '@/utils/axios'
import type {
  AxiosLoginDataTypes,
  AxiosUserMenuTypes,
  AxiosRequsetAdmintorListTypes
} from './types'

export * from './types'

// 登录
export const admintorLogin: (
  name: string,
  pass: string
) => Promise<
  AxiosResponse<AxiosDefaultTypes<AxiosLoginDataTypes>, any>
> = async (name: string, pass: string) => {
  return await axios.post<AxiosDefaultTypes<AxiosLoginDataTypes>>(
    '/api/admintor/login',
    {
      name,
      pass
    }
  )
}

// 检测是否登录 保持登录状态
export const admintorCheck: () => Promise<
  AxiosResponse<AxiosDefaultTypes<AxiosLoginDataTypes>, any>
> = async () => {
  return await axios.get<AxiosDefaultTypes<AxiosLoginDataTypes>>(
    '/api/admintor/check'
  )
}

// 请求用户菜单
export const admintorMenu: () => Promise<
  AxiosResponse<AxiosDefaultTypes<AxiosUserMenuTypes>, any>
> = async () => {
  return axios.get<AxiosDefaultTypes<AxiosUserMenuTypes>>(
    '/api/admintor/router'
  )
}

// 修改用户资料
export const admintorUpdateInfo: ({
  userID,
  name,
  email,
  phone,
  occupation,
  des
}: {
  userID: string
  name: string
  email: string
  phone: string
  occupation: string
  des: string
}) => Promise<AxiosResponse<AxiosDefaultTypes<AxiosLoginDataTypes>, any>> = ({
  userID,
  name,
  email,
  phone,
  occupation,
  des
}) => {
  return axios.post<AxiosDefaultTypes<AxiosLoginDataTypes>>(
    '/api/admintor/update',
    {
      userID,
      name,
      email,
      phone,
      des,
      occupation
    }
  )
}

// 修改密码
export const admintorUpdatePass: ({
  userID,
  oldPass,
  pass
}: {
  userID: any
  oldPass: any
  pass: any
}) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = ({
  userID,
  oldPass,
  pass
}) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/admintor/updatePass', {
    userID,
    oldPass,
    pass
  })
}

// 删除消息列表的消息
export const admintorDelMes: (
  userID: string,
  mesID: string
) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = (
  userID: string,
  mesID: string
) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/admintor/delMes', {
    userID,
    mesID
  })
}

// 加入或移除消息的星标
export const admintorStartMes: (
  userID: string,
  mesID: string,
  mesIndex: number
) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = (
  userID: string,
  mesID: string,
  mesIndex: number
) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/admintor/startMes', {
    userID,
    mesID,
    mesIndex
  })
}

// 消息列表已读和未读处理
export const admintorReadMes: (
  userID: string,
  mesID: string,
  mesIndex: number,
  ifStart?: boolean
) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = (
  userID: string,
  mesID: string,
  mesIndex: number,
  ifStart = false
) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/admintor/readMes', {
    userID,
    mesID,
    mesIndex,
    ifStart
  })
}

// 获取管理员列表 (全部)
export const getAdmintorList: () => Promise<
  AxiosResponse<AxiosDefaultTypes<AxiosRequsetAdmintorListTypes>, any>
> = () => {
  return axios.get<AxiosDefaultTypes<AxiosRequsetAdmintorListTypes>>(
    '/api/admintor/admintorList'
  )
}

// 获取管理员列表 (分页)
/**
 *
 * @param limit 获取数据个数
 * @param skip 获取数据初始值
 * @returns Promise
 */
export const getAdmintorPageList: (
  limit: number,
  skip: number
) => Promise<
  AxiosResponse<AxiosDefaultTypes<AxiosRequsetAdmintorListTypes>, any>
> = (limit: number, skip: number) => {
  return axios.get<AxiosDefaultTypes<AxiosRequsetAdmintorListTypes>>(
    '/api/admintor/pageList',
    {
      params: {
        limit,
        skip
      }
    }
  )
}

// 添加管理员
/**
 *
 * @param name 用户名
 * @param email 邮箱
 * @param pass 密码
 * @param notify 是否通知
 * @returns Promise
 */
export const addAdmintor: (
  name: string,
  email: string,
  pass: string,
  notify: boolean
) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = (
  name: string,
  email: string,
  pass: string,
  notify: boolean
) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/admintor/add', {
    name,
    email,
    pass,
    notify
  })
}

// 搜索获取管理员列表
export const searchAdmintorList: (
  name: string
) => Promise<
  AxiosResponse<AxiosDefaultTypes<AxiosRequsetAdmintorListTypes>, any>
> = (name: string) => {
  return axios.get<AxiosDefaultTypes<AxiosRequsetAdmintorListTypes>>(
    '/api/admintor/searchList',
    {
      params: { name }
    }
  )
}

// 修改用户权限
/**
 *
 * @param userID 用户ID
 * @param type 权限类别 0 => 删除 1=> 发布  2 => 修改
 * @returns
 */
export const updateAdmintorAuthority: (
  userID: string,
  type: number
) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = (
  userID: string,
  type: number
) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/admintor/updateAuthority', {
    userID,
    type
  })
}

// 删除管理员
/**
 *
 * @param userID 用户ID
 * @returns
 */
export const deleteAdmintor: (
  userID: string
) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = (
  userID: string
) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/admintor/delete', {
    userID
  })
}
